/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.zakariya.stickyheadersapp.adapter;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.app.Context;
import org.zakariya.stickyheaders.holder.ItemHolder;
import org.zakariya.stickyheaders.model.ItemModel;
import org.zakariya.stickyheaders.model.Model;

import java.util.List;
/**
 * StickyAboutAdapter
 *
 * @since 2021-08-20
 */
public class StickyAboutAdapter extends BaseItemProvider {
    private Context context;
    private List<ItemModel> data;

    /**
     * Sticky滑动列表构造函数
     *
     * @param context    上下文
     * @param dataSource 数据源
     */
    public StickyAboutAdapter(Context context, List<ItemModel> dataSource) {
        this.context = context;
        this.data = dataSource;
    }

    @Override
    public int getCount() {
        return data != null ? data.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return data != null ? data.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
        Model model = data.get(position);
        Component itemView = null;
        itemView = LayoutScatter.getInstance(context).parse(model.getResource(position), null, false);
        if (itemView != null) {
            ItemHolder itemHolder = new ItemHolder(context, itemView);
            itemHolder.itemText.setText(model.getMessage());
            itemHolder.itemTitle.setText(model.getTitle());
        }
        return itemView;
    }
}
